package cn.xiaowenjie.controllers;

import cn.xiaowenjie.beans.PageDTO;
import com.alibaba.druid.util.StringUtils;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

import cn.xiaowenjie.beans.Contentmanager;
import cn.xiaowenjie.beans.PageObjectContent;
import cn.xiaowenjie.boss.form.ContentmanagerForm;
import cn.xiaowenjie.common.beans.ResultBean;
import cn.xiaowenjie.response.PageResult;
import cn.xiaowenjie.services.ContentManagerService;
import io.swagger.annotations.Api;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by Administrator on 2020/4/10.
 * <p>
 * by author wz
 * <p>
 * cn.xiaowenjie.controllers
 */
@Api(description = "内容管理api")
@RequestMapping("/content/manager")
@RestController
@CrossOrigin
public class ContentManagerController {

    @Autowired
    private ContentManagerService advertManagerService;

    @PostMapping("/find/{id}")
    public ResultBean<Map<String, Object>> getAll(@PathVariable(name = "id") Long id) {
        Map<String, Object> map = new HashMap<>();
        List<Contentmanager> list = Lists.newArrayList();
        List<Contentmanager> collect = Lists.newArrayList();
        Contentmanager contentmanager = advertManagerService.find(id);
        if (contentmanager != null && !StringUtils.isEmpty(contentmanager.getCategoryname()) && !StringUtils.isEmpty(contentmanager.getGrade())) {
            PageObjectContent pageObjectContent = new PageObjectContent();
            pageObjectContent.setCategoryname(contentmanager.getCategoryname());
            pageObjectContent.setGrade(contentmanager.getGrade());
            list = advertManagerService.queryAll(pageObjectContent);
            if (list.size() > 0) {
                collect = list.stream().filter(c -> contentmanager.getId()!=c.getId())
                        .collect(Collectors.toList());
            }
        }
        map.put("currentData", contentmanager);
        map.put("relatedList", collect);
        return new ResultBean<Map<String, Object>>(map);
    }

    @PostMapping("/all")
    public ResultBean<PageResult<Contentmanager>> getAll(@RequestBody PageObjectContent pageObject) {
        return new ResultBean<PageResult<Contentmanager>>(advertManagerService.getAll(pageObject));
    }
    @RequestMapping("/all/admin")
    public ResultBean<PageResult<Contentmanager>> getAllAmin(@RequestBody PageObjectContent pageObject) {
        PageDTO pageDTO = pageObject.getPage();
        if(pageDTO==null){
            pageObject.setPageNo(1);
            pageObject.setPageSize(20);
        }else {
            pageObject.setPageNo(pageDTO.getPageNum());
            pageObject.setPageSize(pageDTO.getPageSize());
        }
        return new ResultBean<PageResult<Contentmanager>>(advertManagerService.getAll(pageObject));
    }

    /**
     * 新增配置
     * <p>
     * FIXME 同时支持json格式和表单格式
     *
     * @return
     */
    @PostMapping("/add")
    public ResultBean<Long> add(@RequestBody @Valid ContentmanagerForm advertForm) {
        return new ResultBean<Long>(advertManagerService.add(advertForm));
    }

    @PostMapping("/delete")
    public ResultBean<Boolean> delete(@RequestParam long id) {
        return new ResultBean<Boolean>(advertManagerService.delete(id));
    }

    @PostMapping("/update")
    public ResultBean<Long> update(@RequestBody @Valid Contentmanager advert) {
        return new ResultBean<Long>(advertManagerService.update(advert));
    }

}
